package com.ssy.controller;

import cn.hutool.core.util.StrUtil;
import com.ssy.common.Result;
import com.ssy.entity.User;
import com.ssy.service.StudentService;
import com.ssy.service.impl.StudentServiceImpl;
import com.ssy.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * StudentServlet
 *
 * @author ssy
 * @date 2020/11/16
 **/
@WebServlet(urlPatterns = {"/student.do"})
public class StudentServlet extends HttpServlet {

    private static final long serialVersionUID = 159420510440967821L;

    StudentService studentService = new StudentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("studentList".equals(type)) {
            studentList(req, resp);
        } else if ("major".equals(type)) {
            major(req, resp);
        } else if ("className".equals(type)) {
            className(req, resp);
        } else if ("addStudent".equals(type)) {
            addStudent(req, resp);
        } else if ("updateStudent".equals(type)) {
            updateStudent(req, resp);
        } else if ("bathDelStudent".equals(type)) {
            bathDelStudent(req, resp);
        } else if ("delOneStudent".equals(type)) {
            delOneStudent(req, resp);
        } else if ("studentInfo".equals(type)) {
            studentInfo(req, resp);
        } else if ("curriculumInfo".equals(type)) {
            curriculumInfo(req, resp);
        } else if ("scoreInfo".equals(type)) {
            scoreInfo(req, resp);
        }
    }

    /**
     * 成绩信息
     *
     * @param req
     * @param resp
     */
    private void scoreInfo(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        String number = user.getUname();
        Result result = this.studentService.selectScoreInfo(number);
        RespUtil.write(resp, result);
    }

    /**
     * 课程表信息
     *
     * @param req
     * @param resp
     */
    private void curriculumInfo(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        String number = user.getUname();
        Result result = this.studentService.selectcurriculumInfo(number);
        RespUtil.write(resp, result);
    }

    /**
     * 查询学生信息
     *
     * @param req
     * @param resp
     */
    private void studentInfo(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        String number = user.getUname();
        Result result = this.studentService.selectStudentInfo(number);
        RespUtil.write(resp, result);
    }

    /**
     * 删除单个学生
     *
     * @param req
     * @param resp
     */
    private void delOneStudent(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Result rs = this.studentService.deleteOneStudent(id);
        RespUtil.write(resp, rs);
    }

    /**
     * 批量删除学生
     *
     * @param req
     * @param resp
     */
    private void bathDelStudent(HttpServletRequest req, HttpServletResponse resp) {
        String[] ids = req.getParameterValues("id");
        Result result = this.studentService.deteleUser(ids);
        RespUtil.write(resp, result);
    }

    /**
     * 修改学生信息
     *
     * @param req
     * @param resp
     */
    private void updateStudent(HttpServletRequest req, HttpServletResponse resp) {
        String sId = req.getParameter("sId");
        String number = req.getParameter("number");
        String sname = req.getParameter("sname");
        String major2 = req.getParameter("major2");
        String className = req.getParameter("className");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        String remarks = req.getParameter("remarks");
        Result rs = this.studentService.updateStudent(sId, number, sname, major2, className, sex, birthday, address, phone, remarks);
        RespUtil.write(resp, rs);
    }

    /**
     * 添加学生
     *
     * @param req
     * @param resp
     */
    private void addStudent(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String sname = req.getParameter("sname");
        String major2 = req.getParameter("major2");
        String className = req.getParameter("className");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        String remarks = req.getParameter("remarks");
        Result rs = this.studentService.addStudent(number, sname, major2, className, sex, birthday, address, phone, remarks);
        RespUtil.write(resp, rs);
    }

    /**
     * 查询所有班级名称
     *
     * @param req
     * @param resp
     */
    private void className(HttpServletRequest req, HttpServletResponse resp) {
        Result rs = this.studentService.queryClassNameList();
        RespUtil.write(resp, rs);
    }

    /**
     * 查询所有专业
     *
     * @param req
     * @param resp
     */
    private void major(HttpServletRequest req, HttpServletResponse resp) {
        Result rs = this.studentService.queryMajorList();
        RespUtil.write(resp, rs);
    }


    /**
     * 学生列表
     *
     * @param req
     * @param resp
     */
    private void studentList(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String majorId = req.getParameter("major");
        String className = req.getParameter("className");
        String phone = req.getParameter("phone");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        HashMap<String, Object> param = new HashMap<>();
        if (StrUtil.isNotBlank(number)) {
            param.put("number", number);
        }
        if (StrUtil.isNotBlank(sname)) {
            param.put("sname", sname);
        }
        if (StrUtil.isNotBlank(sex)) {
            param.put("sex", sex);
        }
        if (StrUtil.isNotBlank(majorId)) {
            param.put("majorId", majorId);
        }
        if (StrUtil.isNotBlank(className)) {
            param.put("className", className);
        }
        if (StrUtil.isNotBlank(phone)) {
            param.put("phone", phone);
        }
        Result result = this.studentService.queryPage(param, page, limit);
        RespUtil.write(resp, result);
    }


}
